package com.example.bookshelf.ui.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.example.bookshelf.R
import com.example.bookshelf.network.Book

@Composable
fun HomeScreen(
    bookShelfState: BookShelfState,
    retryAction: () -> Unit,
    contentPadding: PaddingValues,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .fillMaxSize()
            .padding(contentPadding)
    ) {
        when (bookShelfState) {
            is BookShelfState.Loading -> LoadingScreen(modifier = Modifier.fillMaxSize())
            is BookShelfState.Success -> BookShelfScreen(bookShelfState.books)
            is BookShelfState.Error -> ErrorScreen(retryAction, modifier = Modifier.fillMaxSize())
        }
    }
}

@Composable
private fun LoadingScreen(modifier: Modifier = Modifier) {
    Image(
        painter = painterResource(R.drawable.loading_img),
        contentDescription = stringResource(id = R.string.loading),
        modifier = modifier.size(200.dp)
    )
}

@Composable
private fun ErrorScreen(
    retryAction: () -> Unit,
    modifier: Modifier = Modifier
) {
    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = modifier
    ) {
        Image(
            painter = painterResource(R.drawable.ic_connection_error),
            contentDescription = null
        )
        Text(
            text = stringResource(id = R.string.failed_loading),
            modifier = Modifier.padding(dimensionResource(id = R.dimen.padding_small))
        )
        Button(onClick = retryAction) {
            Text(text = stringResource(id = R.string.retry))
        }
    }
}

@Composable
private fun BookShelfScreen(
    books: List<Book>,
    modifier: Modifier = Modifier
) {
    LazyVerticalStaggeredGrid(
        columns = StaggeredGridCells.Fixed(2),
        verticalItemSpacing = dimensionResource(id = R.dimen.grid_padding),
        horizontalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.grid_padding)),
        modifier = modifier,
        content = {
            items(items = books, key = { book -> book.id }) { book ->
                BookCard(book)
            }
        }
    )
}

@Composable
private fun BookCard(
    book: Book,
    modifier: Modifier = Modifier
) {
    AsyncImage(
        model = ImageRequest.Builder(context = LocalContext.current)
            .data(book.imgUrl)
            .crossfade(true)
            .build(),
        contentDescription = book.title,
        placeholder = painterResource(id = R.drawable.loading_img),
        error = painterResource(id = R.drawable.ic_broken_image),
        contentScale = ContentScale.Crop,
        modifier = modifier.fillMaxSize()
    )
}

// 测试瀑布流布局
//@Composable
//fun WaterfallLayoutScreen() {
//    val items = (1..20).map {
//        // 模拟不同高度的 item
//        if (it % 2 > 0) {
//            Item("Item $it", (100 + it * 20).dp)
//        } else {
//            Item("Item $it", (100 + it * 10).dp)
//        }
//    }
//
//    LazyVerticalStaggeredGrid(
//        columns = StaggeredGridCells.Fixed(2), // 两列
//        verticalItemSpacing = 8.dp,
//        horizontalArrangement = Arrangement.spacedBy(8.dp)
//    ) {
//        items(items) { item ->
//            WaterfallGridItem(item)
//        }
//    }
//}
//
//@Composable
//fun WaterfallGridItem(item: Item) {
//    // 根据高度决定占据的行数
//    Box(
//        modifier = Modifier
//            .fillMaxWidth()
//            .height(item.height)
//            .background(Color.LightGray)
//    ) {
//        Text(
//            text = item.text,
//            modifier = Modifier.align(Alignment.Center)
//        )
//    }
//}
//
//data class Item(val text: String, val height: Dp)
//
//@Preview
//@Composable
//fun WaterfallLayoutScreenPreview() {
//    WaterfallLayoutScreen()
//}